package com.ke.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Bochen
 */
public class AliPayUtils {
    public static String pay(Map<String, Object> map){
        //获得初始化的AlipayClient
        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do","2021000117656483", "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC7pPgwfQwaXxlq8Jas8Gj9uIoeRwPOYAIKerpOd59x+uVe36+QP/Z5gi6UNhryqZZrmuKpIet/1KvxMpUyrjVX3IQXK1qFbQD1Z5ZAHtWBBmhT0WMYwCIHJWfDxYnCPb9bM8/A/tLOWLYyj4fbSki1YvnhmE4E3cGNbRZlyc6sHlWqm9ErxWH6oAaqK407F4NRU09pQ1EaSehvl8LAtNF0JDtI2zdA08oYavZTfYqjCkcT2iEpzzSBbgAzY/RWyjB2Cw42iSlOTm/DzN0GxSPLgSSaLXB6YfxvEwpy9H0NAoEdAI35F42/4NeOplTEUu/isom+Z51XjeAUQ1mQWsURAgMBAAECggEAVlqFHUtu1nQNbnlp3gyFRL44Uda8maz1Q31XX+UNK5cbF545XVkSRLj0VqDPpf0FjF+rD8rcZeepeUaeAsTJJ7YD0+XPsreHSWhybO/Dsa0RNgGWaT2Ca2d7aUzvKMTh20bsxRSATxXkQ0BqdTCqZU83T+qXfQXQnRUmyZEl228QiGuT+UHGLDELfHJg7m8bgraLsZBq9Ca/7LKh4u057ITU7osWYRWEwaULPy+u7Yn6m9/kd5qF/89Jxvgyk4XHqppRsjePJgOAP4IwujHQwa0ZlQJUrBKJWzB6AJFNGBGeOoqgMq00zS9BE79hFSgLNjy/lnARFcopqA3i++HEAQKBgQDiSikzbx8oepnK91gUr77FEDsbkHWbCS8th7Znp87g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4p7VLMlNKSV6EBdq4YeXouTYyXxemM7roYDGt3rVtFmo9/zA3oZXz1Klcif6oEDn06i9b7xYEgFVrJb8s8SJ7Hwg9oVXNg0EDCB6XoD90yDliHJ2QIkwtPjTlJIsQaHLxiCKH4A8SuwIDAQAB", "RSA2");
        //创建API对应的request
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        //同步回调后显示的页面(前端)
        alipayRequest.setReturnUrl( "http://24v6510r56.zicp.vip:17363/#/order" );
        //异步回调显示状态信息
        alipayRequest.setNotifyUrl( "http://bn28gm.natappfree.cc/ali/notifyPay" );
        Map payMap  =new HashMap<>();
        payMap.put("out_trade_no",map.get("orderid"));
        payMap.put("product_code","FAST_INSTANT_TRADE_PAY");
        payMap.put("total_amount",map.get("totalPrice"));
        payMap.put("subject","氪媒体商城");
        payMap.put("body","氪媒体出品");

        //将map转为 json格式
        String string = JSONObject.toJSONString(payMap);
        alipayRequest.setBizContent(string);
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }

    // 将request中的参数转换成Map
    public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();

        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;

            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }

        return retMap;
    }
}
